<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek API 调用示例</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f7fa;
            color: #333;
        }
        .container {
            background-color: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #2d5be3;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
        }
        input[type="text"], textarea {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
            box-sizing: border-box;
        }
        textarea {
            min-height: 120px;
            resize: vertical;
        }
        button {
            background-color: #2d5be3;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            width: 100%;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #1a46c4;
        }
        button:disabled {
            background-color: #a0a0a0;
            cursor: not-allowed;
        }
        .result {
            margin-top: 25px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 6px;
            border-left: 4px solid #2d5be3;
        }
        .error {
            color: #d93025;
            background-color: #fce8e6;
            padding: 12px;
            border-radius: 6px;
            margin-top: 15px;
            display: none;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        .loading-spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #2d5be3;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .api-key-container {
            position: relative;
        }
        .toggle-visibility {
            position: absolute;
            right: 10px;
            top: 38px;
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            width: auto;
            padding: 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>DeepSeek API 调用示例</h1>
        
        <div class="form-group">
            <label for="api-key">API 密钥</label>
            <div class="api-key-container">
                <input type="password" id="api-key" placeholder="请输入您的 DeepSeek API 密钥">
                <button class="toggle-visibility" id="toggle-visibility">显示</button>
            </div>
        </div>
        
        <div class="form-group">
            <label for="prompt">输入您的消息</label>
            <textarea id="prompt" placeholder="请输入您想要询问的内容..."></textarea>
        </div>
        
        <button id="submit-btn">发送请求</button>
        
        <div class="loading" id="loading">
            <div class="loading-spinner"></div>
            <p>正在处理请求，请稍候...</p>
        </div>
        
        <div class="error" id="error"></div>
        
        <div class="result" id="result" style="display: none;">
            <h3>API 响应：</h3>
            <div id="response-content"></div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const apiKeyInput = document.getElementById('api-key');
            const promptInput = document.getElementById('prompt');
            const submitBtn = document.getElementById('submit-btn');
            const loadingElement = document.getElementById('loading');
            const errorElement = document.getElementById('error');
            const resultElement = document.getElementById('result');
            const responseContent = document.getElementById('response-content');
            const toggleVisibilityBtn = document.getElementById('toggle-visibility');
            
            // 切换API密钥可见性
            toggleVisibilityBtn.addEventListener('click', function() {
                if (apiKeyInput.type === 'password') {
                    apiKeyInput.type = 'text';
                    toggleVisibilityBtn.textContent = '隐藏';
                } else {
                    apiKeyInput.type = 'password';
                    toggleVisibilityBtn.textContent = '显示';
                }
            });
            
            // 提交表单事件
            submitBtn.addEventListener('click', async function() {
                const apiKey = apiKeyInput.value.trim();
                const prompt = promptInput.value.trim();
                
                // 验证输入
                if (!apiKey) {
                    showError('请输入API密钥');
                    return;
                }
                
                if (!prompt) {
                    showError('请输入要发送的消息');
                    return;
                }
                
                // 隐藏之前的错误和结果
                hideError();
                hideResult();
                
                // 显示加载状态
                showLoading();
                disableForm();
                
                try {
                    // 调用DeepSeek API
                    const response = await callDeepSeekAPI(apiKey, prompt);
                    
                    // 显示结果
                    showResult(response);
                } catch (error) {
                    showError(error.message);
                } finally {
                    // 隐藏加载状态
                    hideLoading();
                    enableForm();
                }
            });
            
            // 调用DeepSeek API的函数
            async function callDeepSeekAPI(apiKey, prompt) {
                // API端点（请根据DeepSeek官方文档更新此URL）
                const apiUrl = 'https://api.deepseek.com/v1/chat/completions';
                
                const requestBody = {
                    model: 'deepseek-chat', // 根据可用模型调整
                    messages: [
                        {
                            role: 'user',
                            content: prompt
                        }
                    ],
                    temperature: 0.7,
                    max_tokens: 2000
                };
                
                const response = await fetch(apiUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': `Bearer ${apiKey}`
                    },
                    body: JSON.stringify(requestBody)
                });
                
                if (!response.ok) {
                    const errorData = await response.json().catch(() => ({}));
                    throw new Error(errorData.error?.message || `API请求失败: ${response.status} ${response.statusText}`);
                }
                
                const data = await response.json();
                
                if (!data.choices || !data.choices[0] || !data.choices[0].message) {
                    throw new Error('API返回的数据格式不正确');
                }
                
                return data.choices[0].message.content;
            }
            
            // UI辅助函数
            function showLoading() {
                loadingElement.style.display = 'block';
            }
            
            function hideLoading() {
                loadingElement.style.display = 'none';
            }
            
            function showError(message) {
                errorElement.textContent = message;
                errorElement.style.display = 'block';
            }
            
            function hideError() {
                errorElement.style.display = 'none';
            }
            
            function showResult(content) {
                responseContent.textContent = content;
                resultElement.style.display = 'block';
            }
            
            function hideResult() {
                resultElement.style.display = 'none';
            }
            
            function disableForm() {
                submitBtn.disabled = true;
                apiKeyInput.disabled = true;
                promptInput.disabled = true;
            }
            
            function enableForm() {
                submitBtn.disabled = false;
                apiKeyInput.disabled = false;
                promptInput.disabled = false;
            }
        });
    </script>
</body>
</html>
